package xyz.gpgl.erp.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import common.util.base.CommonUtil;
import common.util.base.Result;
import common.util.mybatis.plus.layui.Page;
import common.util.mybatis.plus.logic.LogicBaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.gpgl.erp.sys.entity.SysUser;
import xyz.gpgl.erp.sys.entity.emeus.SysUserEmeus;
import xyz.gpgl.erp.sys.service.ISysUserService;
import xyz.gpgl.erp.sys.service.impl.MyUserDetailServiceImpl;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liubo
 * @since 2019-10-31
 */
@Slf4j
@Controller
@RequestMapping("/sys/sys_user")
public class SysUserController extends LogicBaseController<ISysUserService, SysUser>{

    @Autowired
    private  ISysUserService iSysUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @RequestMapping("/listPage")
    public Object listPage() {
        return "sysUser_list";
    }

    @ResponseBody
    @RequestMapping("/listData")
    public Object listData(SysUser user, Page<SysUser> page) {
        QueryWrapper<SysUser> query= new QueryWrapper<>();
        if(CommonUtil.isNotEmpty(user.getName())) {
            query.like(SysUserEmeus.name, user.getName().trim());
        }
        if(CommonUtil.isNotEmpty(user.getPhone())) {
            query.like(SysUserEmeus.phone, user.getPhone().trim());
        }
        return Result.success(iSysUserService.page(page, query));
    }

    @RequestMapping("/editPage")
    public Object editPage(String id, Model model) {
        if(CommonUtil.isNotEmpty(id)) {
            SysUser row = this.iSysUserService.getById(id);
            if(CommonUtil.isNotEmpty(row)) {
                model.addAttribute("entity",row);
            }
        }
        return "sysUser_edit";
    }
    @ResponseBody
    @RequestMapping("/edit")
    public Object editPage(SysUser user) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SysUserEmeus.phone, user.getPhone());
        if(CommonUtil.isEmpty(user.getId())) {
            user.setPassword(passwordEncoder.encode(CommonUtil.isEmptyReturn(user.getPassword(), "123456")));
        }else {
            queryWrapper.ne(SysUserEmeus.id, user.getId());
            if(CommonUtil.isNotEmpty(user.getPassword())) {
                user.setPassword(passwordEncoder.encode(user.getPassword()));
            }
        }
        SysUser old = this.iSysUserService.getOne(queryWrapper);
        if(CommonUtil.isNotEmpty(old)) {
            return Result.fail("电话号码已存在,请确认");
        }
        log.info("开始保存数据:{}" ,user);
        this.iSysUserService.saveOrUpdate(user);
        return Result.success();
    }

    @ResponseBody
    @RequestMapping("/removed")
    public Object removed(SysUser info) {
        this.iSysUserService.removeById(info.getId());
        return Result.success();
    }

    @RequestMapping("/modifyPassword")
    public Object modifyPassword(Model model) {
        try {
            MyUserDetailServiceImpl.MyUserDetails user = (MyUserDetailServiceImpl.MyUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            SysUser row = this.iSysUserService.getOne(new QueryWrapper<SysUser>().eq(SysUserEmeus.phone, user.getUsername()));
            model.addAttribute("entity",row);
            return "sysUser_edit_password";
        } catch (Exception e) {
            return "/";
        }
    }

}
